﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Noftware.Com.Cryptography;

namespace Noftware.Com.Cryptographer.UI
{
    /// <summary>
    /// Form to allow the user to specify attributes about the key/salt.
    /// </summary>
    public partial class KeyGenerator : Form
    {
        #region Private variables

        /// <summary>
        /// Key settings.
        /// </summary>
        private KeySettings _settings = new KeySettings();

        #endregion

        #region Constructors

        /// <summary>
        /// Default constructor.
        /// </summary>
        public KeyGenerator()
        {
            InitializeComponent();
        }

        #endregion

        #region Public properties

        /// <summary>
        /// Returns the user's chosen Key.
        /// </summary>
        public string KeyValue
        {
            get
            {
                return this.txtKey.Text;
            }
        }

        /// <summary>
        /// Only allows numeric keys (i.e. for XOR).
        /// </summary>
        public bool NumericOnly { get; set; }

        #endregion Public properties

        /// <summary>
        /// Event handler of the Close form button.
        /// </summary>
        private void cmdCloseForm_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        /// <summary>
        /// Event handler called upon page load.
        /// </summary>
        private void KeyGenerator_Load(object sender, EventArgs e)
        {
            // Set defaults
            _settings.KeyLength = 8;
            _settings.KeyType = KeyType.AlphaNumeric;
            _settings.KeyCase = KeyCase.Mixed;
            this.txtKeyLength.Value = _settings.KeyLength;

            // Numeric only?
            this.setOptions();
        }

        /// <summary>
        /// Set form options (numeric-only or not).
        /// </summary>
        private void setOptions()
        {
            // Numeric only? If so, disabled certain options
            if (this.NumericOnly == true)
            {
                // Numeric only
                rdoAlphaNumeric.Enabled = false;
                rdoAlphaOnly.Enabled = false;
                rdoGuid.Enabled = false;
                grpAlphaCase.Enabled = false;
                rdoNumericOnly.Checked = true;
            }
            else
            {
                // All modes enabled
                rdoAlphaNumeric.Enabled = true;
                rdoAlphaOnly.Enabled = true;
                rdoGuid.Enabled = true;
                grpAlphaCase.Enabled = true;
            }

            txtKey.Text = string.Empty;
        }

        /// <summary>
        /// Event handler of the AlphaNumeric radio button.
        /// </summary>
        private void rdoAlphaNumeric_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoAlphaNumeric.Checked == false)
            {
                return;
            }

            _settings.KeyType = KeyType.AlphaNumeric;
            
            this.NumericOnly = false;
            this.setOptions();
        }

        /// <summary>
        /// Event handler of the Numeric-only radio button.
        /// </summary>
        private void rdoNumericOnly_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoNumericOnly.Checked == false)
            {
                return;
            }

            _settings.KeyType = KeyType.Numeric;

            this.NumericOnly = true;
            this.setOptions();
        }

        /// <summary>
        /// Event handler of the Alpha-only radio button.
        /// </summary>
        private void rdoAlphaOnly_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoAlphaOnly.Checked == false)
            {
                return;
            }

            _settings.KeyType = KeyType.Alpha;

            this.NumericOnly = false;
            this.setOptions();
        }

        /// <summary>
        /// Event handler of the GUID radio button.
        /// </summary>
        private void rdoGuid_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoGuid.Checked == false)
            {
                return;
            }

            _settings.KeyType = KeyType.Guid;

            this.NumericOnly = false;
            this.setOptions();
        }

        /// <summary>
        /// Event handler of the Mixed Case radio button.
        /// </summary>
        private void rdoMixedCase_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoMixedCase.Checked == false)
            {
                return;
            }

            _settings.KeyCase = KeyCase.Mixed;
        }

        /// <summary>
        /// Event handler of the Lower Case radio button.
        /// </summary>
        private void rdoLowerCase_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoLowerCase.Checked == false)
            {
                return;
            }
            _settings.KeyCase = KeyCase.Lower;
        }

        /// <summary>
        /// Event handler of the Upper Case radio button.
        /// </summary>
        private void rdoUpperCase_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoUpperCase.Checked == false)
            {
                return;
            }

            _settings.KeyCase = KeyCase.Upper;
        }

        /// <summary>
        /// Event handler of the Generate button.
        /// </summary>
        private void cmdGenerate_Click(object sender, EventArgs e)
        {
            var gen = new Generator();
            _settings.KeyLength = (int)txtKeyLength.Value;
            txtKey.Text = gen.Generate(_settings);
        }
    }
}
